﻿/*
 *  PirateShare - a tool for fair&legal content sharing among friends.  
 *  see <http://pirateshare.codeplex.com> for latest version.
 *
 *  Copyright (C) 2010 Ilya Elenskiy
 *
 *  This program is free software; you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation; either version 2 of the License, or 
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License 
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PirateShare
{
    [Serializable]
    public class SharedFile
    {
        public string Name { get; private set; }
        public long Size { get; private set; }
        public string Hash { get; private set; }

        long SegmentsNumber { get { return (long)Math.Ceiling((double)Size / (double)Globals.FileSegmentSize); } }

        public string HumanReadableSize
        {
            get
            {
                               
                if (Size < 1024)
                {
                    return( Size + "b");
                }
                else if (Size < 1024*1024)
                {
                    return( Math.Round( (double)Size/1024.0, 1) + "kb");
                }
                else if (Size < 1024*1024*1024)
                {
                    return (Math.Round((double)Size / (1024.0 * 1024.0), 1) + "Mb");
                }
                else if (Size < 1024.0 * 1024.0 * 1024.0 * 1024.0)
                {
                    return (Math.Round((double)Size / (1024.0 * 1024.0 * 1024), 1) + "Gb");
                }

                return Size.ToString();
            }
        }

        public string Type
        {
            get
            {
                return Path.GetExtension(Name);
            }
        }




        public SharedFile(string name, long size, string hash)
        {
            this.Name = name;
            this.Size = size;
            this.Hash = hash;
        }

        public SharedFile(string fileName)
        {
            this.Name = Path.GetFileName(fileName);

            FileInfo fi = new FileInfo(fileName);
            this.Size = fi.Length;

            this.Hash = CryptoHelper.FileHash(fileName);
        }


        public override string ToString()
        {
            return Name;
        }
    }
}
